#include <iostream>
#include <stack>
#include <sstream>
using namespace std;
stack<int> mystack;
string result[10001];
int jishu=0;
int main()
{
	int n;
	while(cin>>n){
		if(n==0) break;
		for(int i=0;i<n;i++){
			char order;
			cin>>order;
			switch(order){
				case 'P':
					int num;
					cin>>num;
					mystack.push(num);
					break;
				case 'O':
					if(!mystack.empty()){
						mystack.pop();
						break;
					}
					else break;
				case 'A':
					if(!mystack.empty()){
						int temp=mystack.top();
						string kkk;
						stringstream ss;
						ss<<temp;
						ss>>kkk;
					
						result[jishu]=kkk;
						jishu++;
						break;
					}
					else {
						result[jishu]='E';
						jishu++;
					}
					
			}
		}
		while(!mystack.empty()){
			mystack.pop();
		}
		for(int h=0;h<jishu;h++) cout<<result[h]<<endl;
		cout<<endl;
		jishu=0;
	}
}
